
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>php函数 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="10_php面向对象.html" />
    
    
    <link rel="prev" href="08_php数组.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="01_php介绍.html">
            
                <a href="01_php介绍.html">
            
                    
                    php基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="01_php介绍.html">
            
                <a href="01_php介绍.html">
            
                    
                    php介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="02_PHP基础语法.html">
            
                <a href="02_PHP基础语法.html">
            
                    
                    php基础语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="03_php变量.html">
            
                <a href="03_php变量.html">
            
                    
                    php变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="04_php数据类型.html">
            
                <a href="04_php数据类型.html">
            
                    
                    php数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="05_php常量.html">
            
                <a href="05_php常量.html">
            
                    
                    php常量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="06_php运算符.html">
            
                <a href="06_php运算符.html">
            
                    
                    php运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="07_php流程控制.html">
            
                <a href="07_php流程控制.html">
            
                    
                    php流程控制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="08_php数组.html">
            
                <a href="08_php数组.html">
            
                    
                    php数组
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2.9" data-path="09_php函数.html">
            
                <a href="09_php函数.html">
            
                    
                    php函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.10" data-path="10_php面向对象.html">
            
                <a href="10_php面向对象.html">
            
                    
                    php面向对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.11" data-path="11_php命名空间.html">
            
                <a href="11_php命名空间.html">
            
                    
                    php命名空间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.12" data-path="12_php日期和时间.html">
            
                <a href="12_php日期和时间.html">
            
                    
                    php日期时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.13" data-path="13_php表单.html">
            
                <a href="13_php表单.html">
            
                    
                    php表单
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.14" data-path="14_php包含文件.html">
            
                <a href="14_php包含文件.html">
            
                    
                    php包含文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.15" data-path="15_php常用函数.html">
            
                <a href="15_php常用函数.html">
            
                    
                    php常用函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.16" data-path="../php高级/16_php常用变量函数.md">
            
                <span>
            
                    
                    php常用变量函数
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../php高级/01_php错误与异常.html">
            
                <a href="../php高级/01_php错误与异常.html">
            
                    
                    php错误与异常
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../php高级/02_PhpStorm本地断点调试.html">
            
                <a href="../php高级/02_PhpStorm本地断点调试.html">
            
                    
                    PhpStorm本地断点调试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../php高级/03_php文件操作.html">
            
                <a href="../php高级/03_php文件操作.html">
            
                    
                    php文件操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../php高级/04_cookie机制.html">
            
                <a href="../php高级/04_cookie机制.html">
            
                    
                    cookie机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../php高级/05_session.html">
            
                <a href="../php高级/05_session.html">
            
                    
                    session
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../php高级/06_php画图.html">
            
                <a href="../php高级/06_php画图.html">
            
                    
                    php画图
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../php高级/07_php加密机制.html">
            
                <a href="../php高级/07_php加密机制.html">
            
                    
                    php加密机制
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >php函数</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="php&#x51FD;&#x6570;">PHP&#x51FD;&#x6570;</h1>
<blockquote>
<p>PHP &#x7684;&#x771F;&#x6B63;&#x5A01;&#x529B;&#x6E90;&#x81EA;&#x4E8E;&#x5B83;&#x7684;&#x51FD;&#x6570;&#x3002;</p>
</blockquote>
<p>&#x5728; PHP &#x4E2D;&#xFF0C;&#x63D0;&#x4F9B;&#x4E86;&#x8D85;&#x8FC7; 1000 &#x4E2A;&#x5185;&#x5EFA;&#x7684;&#x51FD;&#x6570;&#x3002;</p>
<h2 id="&#x521B;&#x5EFA;&#x51FD;&#x6570;">&#x521B;&#x5EFA;&#x51FD;&#x6570;</h2>
<p>&#x8BED;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-php"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">functionName</span><span class="hljs-params">()</span>
</span>{
&#x8981;&#x6267;&#x884C;&#x7684;&#x4EE3;&#x7801;;
}
</code></pre>
<p>PHP &#x51FD;&#x6570;&#x51C6;&#x5219;&#xFF1A;</p>
<ul>
<li>&#x51FD;&#x6570;&#x7684;&#x540D;&#x79F0;&#x5E94;&#x8BE5;&#x63D0;&#x793A;&#x51FA;&#x5B83;&#x7684;&#x529F;&#x80FD;</li>
<li>&#x51FD;&#x6570;&#x540D;&#x79F0;&#x4EE5;&#x5B57;&#x6BCD;&#x6216;&#x4E0B;&#x5212;&#x7EBF;&#x5F00;&#x5934;&#xFF08;&#x4E0D;&#x80FD;&#x4EE5;&#x6570;&#x5B57;&#x5F00;&#x5934;&#xFF09;</li>
<li>&#x51FD;&#x6570;&#x7684;&#x540D;&#x79F0;&#x4E0D;&#x533A;&#x5206;&#x5927;&#x5C0F;&#x5199;</li>
</ul>
<h2 id="php&#x51FD;&#x6570;&#x4F5C;&#x7528;&#x57DF;">PHP&#x51FD;&#x6570;&#x4F5C;&#x7528;&#x57DF;</h2>
<blockquote>
<p>Note: PHP &#x4E2D;&#x7684;&#x6240;&#x6709;&#x51FD;&#x6570;&#x548C;&#x7C7B;&#x90FD;&#x5177;&#x6709;&#x5168;&#x5C40;&#x4F5C;&#x7528;&#x57DF;&#xFF0C;&#x53EF;&#x4EE5;&#x5B9A;&#x4E49;&#x5728;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x4E4B;&#x5185;&#x800C;&#x5728;&#x4E4B;&#x5916;&#x8C03;&#x7528;&#xFF0C;&#x53CD;&#x4E4B;&#x4EA6;&#x7136;&#x3002;</p>
</blockquote>
<pre><code class="lang-php"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">outer</span><span class="hljs-params">()</span></span>{
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">inner</span><span class="hljs-params">()</span></span>{
        <span class="hljs-keyword">echo</span> <span class="hljs-string">&quot;this is inner fn&quot;</span>;
    }
}
inner(); <span class="hljs-comment">// &#x9519;&#x8BEF; &#x56E0;&#x4E3A;inner&#x8FD8;&#x6CA1;&#x6709;&#x521B;&#x5EFA;</span>

outer(); <span class="hljs-comment">//&#x8C03;&#x7528;outer()  inner() &#x521B;&#x5EFA;</span>

inner(); <span class="hljs-comment">//&#x5DF2;&#x7ECF;&#x521B;&#x5EFA;</span>
</code></pre>
<blockquote>
<p>Note: PHP &#x4E0D;&#x652F;&#x6301;&#x51FD;&#x6570;&#x91CD;&#x8F7D;&#xFF0C;&#x4E5F;&#x4E0D;&#x53EF;&#x80FD;&#x53D6;&#x6D88;&#x5B9A;&#x4E49;&#x6216;&#x8005;&#x91CD;&#x5B9A;&#x4E49;&#x5DF2;&#x58F0;&#x660E;&#x7684;&#x51FD;&#x6570;&#x3002;</p>
<p>Note: &#x51FD;&#x6570;&#x540D;&#x662F;&#x5927;&#x5C0F;&#x5199;&#x65E0;&#x5173;&#x7684;&#xFF0C;&#x4E0D;&#x8FC7;&#x5728;&#x8C03;&#x7528;&#x51FD;&#x6570;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4F7F;&#x7528;&#x5176;&#x5728;&#x5B9A;&#x4E49;&#x65F6;&#x76F8;&#x540C;&#x7684;&#x5F62;&#x5F0F;&#x662F;&#x4E2A;&#x597D;&#x4E60;&#x60EF;&#x3002;</p>
</blockquote>
<pre><code class="lang-php"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Login</span><span class="hljs-params">()</span></span>{
    <span class="hljs-keyword">echo</span> <span class="hljs-string">&quot;is login!&quot;</span>;
}
login();
</code></pre>
<h2 id="&#x51FD;&#x6570;&#x53C2;&#x6570;">&#x51FD;&#x6570;&#x53C2;&#x6570;</h2>
<p>&#x53C2;&#x6570;&#x662F;&#x901A;&#x8FC7;&#x8C03;&#x7528;&#x4EE3;&#x7801;&#x5C06;&#x503C;&#x4F20;&#x9012;&#x7ED9;&#x51FD;&#x6570;&#x7684;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x3002;</p>
<p>&#x53C2;&#x6570;&#x662F;&#x5728;&#x53C2;&#x6570;&#x5217;&#x8868;&#x4E2D;&#x58F0;&#x660E;&#x7684;&#xFF0C;&#x4F5C;&#x4E3A;&#x51FD;&#x6570;&#x58F0;&#x660E;&#x7684;&#x4E00;&#x90E8;&#x5206;&#xFF1A;</p>
<pre><code class="lang-php"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sum</span><span class="hljs-params">($a,$b)</span></span>{
    <span class="hljs-keyword">echo</span> $a+$b;
}
sum(<span class="hljs-number">10</span>,<span class="hljs-number">20</span>);
</code></pre>
<h3 id="&#x901A;&#x8FC7;&#x5F15;&#x7528;&#x4F20;&#x9012;&#x53C2;&#x6570;">&#x901A;&#x8FC7;&#x5F15;&#x7528;&#x4F20;&#x9012;&#x53C2;&#x6570;</h3>
<p>&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x51FD;&#x6570;&#x53C2;&#x6570;&#x901A;&#x8FC7;&#x503C;&#x4F20;&#x9012;&#xFF08;&#x56E0;&#x800C;&#x5373;&#x4F7F;&#x5728;&#x51FD;&#x6570;&#x5185;&#x90E8;&#x6539;&#x53D8;&#x53C2;&#x6570;&#x7684;&#x503C;&#xFF0C;&#x5B83;&#x5E76;&#x4E0D;&#x4F1A;&#x6539;&#x53D8;&#x51FD;&#x6570;&#x5916;&#x90E8;&#x7684;&#x503C;&#xFF09;&#x3002;&#x5982;&#x679C;&#x5E0C;&#x671B;&#x5141;&#x8BB8;&#x51FD;&#x6570;&#x4FEE;&#x6539;&#x5B83;&#x7684;&#x53C2;&#x6570;&#x503C;&#xFF0C;&#x5FC5;&#x987B;&#x901A;&#x8FC7;&#x5F15;&#x7528;&#x4F20;&#x9012;&#x53C2;&#x6570;&#x3002;</p>
<p>&#x5982;&#x679C;&#x60F3;&#x8981;&#x51FD;&#x6570;&#x7684;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x603B;&#x662F;&#x901A;&#x8FC7;&#x5F15;&#x7528;&#x4F20;&#x9012;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x4E2D;&#x8BE5;&#x53C2;&#x6570;&#x7684;&#x524D;&#x9762;&#x52A0;&#x4E0A;&#x7B26;&#x53F7; &amp;&#xFF1A;</p>
<pre><code class="lang-php"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">changeString</span><span class="hljs-params">(&amp;$string)</span></span>{
    $string.=<span class="hljs-string">&quot;this is add string&quot;</span>;
}
$str=<span class="hljs-string">&quot;&#x4F60;&#x597D;&quot;</span>;
changeString($str);
<span class="hljs-keyword">echo</span> $str;
</code></pre>
<h3 id="&#x9ED8;&#x8BA4;&#x53C2;&#x6570;&#x7684;&#x503C;">&#x9ED8;&#x8BA4;&#x53C2;&#x6570;&#x7684;&#x503C;</h3>
<pre><code class="lang-php"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sayName</span><span class="hljs-params">($name=<span class="hljs-string">&quot;input names&quot;</span>)</span></span>{
    <span class="hljs-keyword">echo</span> $name;
}
sayName();
</code></pre>
<h2 id="&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x503C;">&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x503C;</h2>
<blockquote>
<p>&#x5982;&#x9700;&#x8BA9;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x503C;&#xFF0C;&#x8BF7;&#x4F7F;&#x7528; return &#x8BED;&#x53E5;&#x3002;</p>
</blockquote>
<p>&#x503C;&#x901A;&#x8FC7;&#x4F7F;&#x7528;&#x53EF;&#x9009;&#x7684;&#x8FD4;&#x56DE;&#x8BED;&#x53E5;&#x8FD4;&#x56DE;&#x3002;&#x53EF;&#x4EE5;&#x8FD4;&#x56DE;&#x5305;&#x62EC;&#x6570;&#x7EC4;&#x548C;&#x5BF9;&#x8C61;&#x7684;&#x4EFB;&#x610F;&#x7C7B;&#x578B;&#x3002;&#x8FD4;&#x56DE;&#x8BED;&#x53E5;&#x4F1A;&#x7ACB;&#x5373;&#x4E2D;&#x6B62;&#x51FD;&#x6570;&#x7684;&#x8FD0;&#x884C;&#xFF0C;&#x5E76;&#x4E14;&#x5C06;&#x63A7;&#x5236;&#x6743;&#x4EA4;&#x56DE;&#x8C03;&#x7528;&#x8BE5;&#x51FD;&#x6570;&#x7684;&#x4EE3;&#x7801;&#x884C;&#x3002;</p>
<blockquote>
<p>Note:&#x5982;&#x679C;&#x7701;&#x7565;&#x4E86; return&#xFF0C;&#x5219;&#x8FD4;&#x56DE;&#x503C;&#x4E3A; NULL&#x3002;</p>
</blockquote>
<pre><code class="lang-php"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">square</span><span class="hljs-params">($num)</span>
</span>{
    <span class="hljs-keyword">return</span> $num * $num;
}
<span class="hljs-keyword">echo</span> square(<span class="hljs-number">4</span>);   <span class="hljs-comment">// outputs &apos;16&apos;</span>
</code></pre>
<h2 id="&#x53EF;&#x53D8;&#x51FD;&#x6570;">&#x53EF;&#x53D8;&#x51FD;&#x6570;</h2>
<p>PHP &#x652F;&#x6301;&#x53EF;&#x53D8;&#x51FD;&#x6570;&#x7684;&#x6982;&#x5FF5;&#x3002;&#x8FD9;&#x610F;&#x5473;&#x7740;&#x5982;&#x679C;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x540D;&#x540E;&#x6709;&#x5706;&#x62EC;&#x53F7;&#xFF0C;PHP &#x5C06;&#x5BFB;&#x627E;&#x4E0E;&#x53D8;&#x91CF;&#x7684;&#x503C;&#x540C;&#x540D;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x5E76;&#x4E14;&#x5C1D;&#x8BD5;&#x6267;&#x884C;&#x5B83;&#x3002;&#x53EF;&#x53D8;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x5B9E;&#x73B0;&#x5305;&#x62EC;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#xFF0C;&#x51FD;&#x6570;&#x8868;&#x5728;&#x5185;&#x7684;&#x4E00;&#x4E9B;&#x7528;&#x9014;&#x3002;</p>
<pre><code class="lang-php"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">echo</span> <span class="hljs-string">&quot;In foo()&lt;br /&gt;\n&quot;</span>;
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">bar</span><span class="hljs-params">($arg = <span class="hljs-string">&apos;&apos;</span>)</span> </span>{
    <span class="hljs-keyword">echo</span> <span class="hljs-string">&quot;In bar(); argument was &apos;$arg&apos;.&lt;br /&gt;\n&quot;</span>;
}

<span class="hljs-comment">// &#x4F7F;&#x7528; echo &#x7684;&#x5305;&#x88C5;&#x51FD;&#x6570;</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">echoit</span><span class="hljs-params">($string)</span>
</span>{
    <span class="hljs-keyword">echo</span> $string;
}

$func = <span class="hljs-string">&apos;foo&apos;</span>;
$func();        <span class="hljs-comment">// This calls foo()</span>

$func = <span class="hljs-string">&apos;bar&apos;</span>;
$func(<span class="hljs-string">&apos;test&apos;</span>);  <span class="hljs-comment">// This calls bar()</span>

$func = <span class="hljs-string">&apos;echoit&apos;</span>;
$func(<span class="hljs-string">&apos;test&apos;</span>);  <span class="hljs-comment">// This calls echoit()</span>
</code></pre>
<h3 id="&#x533F;&#x540D;&#x51FD;&#x6570;&#x53D8;&#x91CF;&#x8D4B;&#x503C;&#x793A;&#x4F8B;">&#x533F;&#x540D;&#x51FD;&#x6570;&#x53D8;&#x91CF;&#x8D4B;&#x503C;&#x793A;&#x4F8B;</h3>
<pre><code class="lang-php">$greet = <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">($name)</span>
</span>{
    printf(<span class="hljs-string">&quot;Hello %s\r\n&quot;</span>, $name);
};

$greet(<span class="hljs-string">&apos;World&apos;</span>);
$greet(<span class="hljs-string">&apos;PHP&apos;</span>);
</code></pre>
<h3 id="&#x8BBF;&#x95EE;&#x51FD;&#x6570;&#x5916;&#x90E8;&#x53D8;&#x91CF;">&#x8BBF;&#x95EE;&#x51FD;&#x6570;&#x5916;&#x90E8;&#x53D8;&#x91CF;</h3>
<h4 id="global">global</h4>
<p>global &#x5173;&#x952E;&#x8BCD;&#x7528;&#x4E8E;&#x8BBF;&#x95EE;&#x51FD;&#x6570;&#x5916;&#x90E8;&#x7684;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x3002;</p>
<p>&#x5728;&#x51FD;&#x6570;&#x5185;&#x8C03;&#x7528;&#x51FD;&#x6570;&#x5916;&#x5B9A;&#x4E49;&#x7684;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5728;&#x51FD;&#x6570;&#x4E2D;&#x7684;&#x53D8;&#x91CF;&#x524D;&#x52A0;&#x4E0A; global &#x5173;&#x952E;&#x5B57;&#xFF1A;</p>
<pre><code class="lang-php">$a = <span class="hljs-number">100</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">num</span><span class="hljs-params">($n)</span></span>{
    <span class="hljs-keyword">global</span> $a;
    <span class="hljs-keyword">echo</span> $n + $a;
}
num(<span class="hljs-number">50</span>);
</code></pre>
<p>PHP &#x5C06;&#x6240;&#x6709;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x5B58;&#x50A8;&#x5728;&#x4E00;&#x4E2A;&#x540D;&#x4E3A;<code>$GLOBALS[index]</code> &#x7684;&#x6570;&#x7EC4;&#x4E2D;&#x3002; index &#x4FDD;&#x5B58;&#x53D8;&#x91CF;&#x7684;&#x540D;&#x79F0;&#x3002;&#x8FD9;&#x4E2A;&#x6570;&#x7EC4;&#x53EF;&#x4EE5;&#x5728;&#x51FD;&#x6570;&#x5185;&#x90E8;&#x8BBF;&#x95EE;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x7528;&#x6765;&#x66F4;&#x65B0;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x3002;</p>
<p>$GLOBALS &#x2014; &#x5F15;&#x7528;&#x5168;&#x5C40;&#x4F5C;&#x7528;&#x57DF;&#x4E2D;&#x53EF;&#x7528;&#x7684;&#x5168;&#x90E8;&#x53D8;&#x91CF; $GLOBALS &#x8FD9;&#x79CD;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x7528;&#x4E8E;&#x5728; PHP &#x811A;&#x672C;&#x4E2D;&#x7684;&#x4EFB;&#x610F;&#x4F4D;&#x7F6E;&#x8BBF;&#x95EE;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#xFF08;&#x4ECE;&#x51FD;&#x6570;&#x6216;&#x65B9;&#x6CD5;&#x4E2D;&#x5747;&#x53EF;&#xFF09;&#x3002; PHP &#x5728;&#x540D;&#x4E3A; $GLOBALS[index] &#x7684;&#x6570;&#x7EC4;&#x4E2D;&#x5B58;&#x50A8;&#x4E86;&#x6240;&#x6709;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x3002;&#x53D8;&#x91CF;&#x7684;&#x540D;&#x5B57;&#x5C31;&#x662F;&#x6570;&#x7EC4;&#x7684;&#x952E;&#x3002;</p>
<h4 id="&#x4ECE;&#x7236;&#x4F5C;&#x7528;&#x57DF;&#x7EE7;&#x627F;&#x53D8;&#x91CF;">&#x4ECE;&#x7236;&#x4F5C;&#x7528;&#x57DF;&#x7EE7;&#x627F;&#x53D8;&#x91CF;</h4>
<p>&#x95ED;&#x5305;&#x53EF;&#x4EE5;&#x4ECE;&#x7236;&#x4F5C;&#x7528;&#x57DF;&#x4E2D;&#x7EE7;&#x627F;&#x53D8;&#x91CF;&#x3002; &#x4EFB;&#x4F55;&#x6B64;&#x7C7B;&#x53D8;&#x91CF;&#x90FD;&#x5E94;&#x8BE5;&#x7528; use &#x8BED;&#x8A00;&#x7ED3;&#x6784;&#x4F20;&#x9012;&#x8FDB;&#x53BB;&#x3002;</p>
<pre><code class="lang-php">$message = <span class="hljs-string">&apos;hello&apos;</span>;
<span class="hljs-comment">// &#x7EE7;&#x627F; $message</span>
$example = <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">()</span> <span class="hljs-title">use</span> <span class="hljs-params">($message)</span> </span>{
    var_dump($message);
};
<span class="hljs-keyword">echo</span> $example();
</code></pre>
<h3 id="static-&#x4F5C;&#x7528;&#x57DF;">Static &#x4F5C;&#x7528;&#x57DF;</h3>
<p>&#x5F53;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x5B8C;&#x6210;&#x65F6;&#xFF0C;&#x5B83;&#x7684;&#x6240;&#x6709;&#x53D8;&#x91CF;&#x901A;&#x5E38;&#x90FD;&#x4F1A;&#x88AB;&#x5220;&#x9664;&#x3002;&#x7136;&#x800C;&#xFF0C;&#x6709;&#x65F6;&#x5019;&#x60A8;&#x5E0C;&#x671B;&#x67D0;&#x4E2A;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x4E0D;&#x8981;&#x88AB;&#x5220;&#x9664;&#x3002;</p>
<p>&#x8981;&#x505A;&#x5230;&#x8FD9;&#x4E00;&#x70B9;&#xFF0C;&#x5728;&#x7B2C;&#x4E00;&#x6B21;&#x58F0;&#x660E;&#x53D8;&#x91CF;&#x65F6;&#x4F7F;&#x7528; static &#x5173;&#x952E;&#x5B57;&#xFF1A;</p>
<pre><code class="lang-php"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">myTest</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-keyword">static</span> $x=<span class="hljs-number">0</span>;
    <span class="hljs-keyword">echo</span> $x;
    $x++;
}

myTest();
myTest();
myTest();
<span class="hljs-comment">//&#x8F93;&#x51FA;&#xFF1A; 0 1 2</span>
</code></pre>
<p>&#x7136;&#x540E;&#xFF0C;&#x6BCF;&#x6B21;&#x8C03;&#x7528;&#x8BE5;&#x51FD;&#x6570;&#x65F6;&#xFF0C;&#x8BE5;&#x53D8;&#x91CF;&#x5C06;&#x4F1A;&#x4FDD;&#x7559;&#x7740;&#x51FD;&#x6570;&#x524D;&#x4E00;&#x6B21;&#x88AB;&#x8C03;&#x7528;&#x65F6;&#x7684;&#x503C;&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x91CA;&#xFF1A;&#x8BE5;&#x53D8;&#x91CF;&#x4ECD;&#x7136;&#x662F;&#x51FD;&#x6570;&#x7684;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x3002;</p>
</blockquote>
<h4 id="static&#x7528;&#x6CD5;">static&#x7528;&#x6CD5;</h4>
<ol>
<li>static&#x653E;&#x5728;&#x51FD;&#x6570;&#x5185;&#x90E8;&#x4FEE;&#x9970;&#x53D8;&#x91CF;</li>
<li>static&#x653E;&#x5728;&#x7C7B;&#x91CC;&#x4FEE;&#x9970;&#x5C5E;&#x6027;&#xFF0C;&#x6216;&#x65B9;&#x6CD5;</li>
<li>static&#x653E;&#x5728;&#x7C7B;&#x7684;&#x65B9;&#x6CD5;&#x91CC;&#x4FEE;&#x9970;&#x53D8;&#x91CF;</li>
<li>static&#x4FEE;&#x9970;&#x5728;&#x5168;&#x5C40;&#x4F5C;&#x7528;&#x57DF;&#x7684;&#x53D8;&#x91CF;</li>
</ol>
<p>1.&#x5728;&#x51FD;&#x6570;&#x6267;&#x884C;&#x5B8C;&#x540E;&#xFF0C;&#x53D8;&#x91CF;&#x503C;&#x4ECD;&#x7136;&#x4FDD;&#x5B58;</p>
<p>&#x5982;&#x4E0B;&#x6240;&#x793A;&#xFF1A;</p>
<pre><code class="lang-php"><span class="hljs-meta">&lt;?php</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">testStatic</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">static</span> $val = <span class="hljs-number">1</span>;
    <span class="hljs-keyword">echo</span> $val;
    $val++;
}
testStatic();   <span class="hljs-comment">//output 1</span>
testStatic();   <span class="hljs-comment">//output 2</span>
testStatic();   <span class="hljs-comment">//output 3</span>
</code></pre>
<p>2.&#x4FEE;&#x9970;&#x5C5E;&#x6027;&#x6216;&#x65B9;&#x6CD5;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x7C7B;&#x540D;&#x8BBF;&#x95EE;&#xFF0C;&#x5982;&#x679C;&#x662F;&#x4FEE;&#x9970;&#x7684;&#x662F;&#x7C7B;&#x7684;&#x5C5E;&#x6027;&#xFF0C;&#x4FDD;&#x7559;&#x503C;</p>
<pre><code class="lang-php"><span class="hljs-meta">&lt;?php</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span> </span>{
    <span class="hljs-keyword">static</span> $id = <span class="hljs-number">0</span>;

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">__construct</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">self</span>::$id++;
    }

    <span class="hljs-keyword">static</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getId</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">self</span>::$id;
    }
}
<span class="hljs-keyword">echo</span> Person::$id;   <span class="hljs-comment">//output 0</span>
<span class="hljs-keyword">echo</span> <span class="hljs-string">&quot;&lt;br/&gt;&quot;</span>;

$p1=<span class="hljs-keyword">new</span> Person();
$p2=<span class="hljs-keyword">new</span> Person();
$p3=<span class="hljs-keyword">new</span> Person();

<span class="hljs-keyword">echo</span> Person::$id;   <span class="hljs-comment">//output 3</span>
<span class="hljs-meta">?&gt;</span>
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="08_php数组.html" class="navigation navigation-prev " aria-label="Previous page: php数组">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="10_php面向对象.html" class="navigation navigation-next " aria-label="Next page: php面向对象">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"php函数","level":"1.2.9","depth":2,"next":{"title":"php面向对象","level":"1.2.10","depth":2,"path":"php/php基础/10_php面向对象.md","ref":"./php/php基础/10_php面向对象.md","articles":[]},"previous":{"title":"php数组","level":"1.2.8","depth":2,"path":"php/php基础/08_php数组.md","ref":"./php/php基础/08_php数组.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"php/php基础/09_php函数.md","mtime":"2020-04-23T09:59:52.389Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-04-23T11:06:38.883Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

